/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
jQuery.noConflict();
describe("player startup", function() {
    beforeEach(function() {
        playerStub = jasmine.createSpyObj("player", ['Play', 'Stop', 'Pause']);
        player = new PlayerStateMachine(playerStub);
    });
    it("should move to waiting", function() {
        player.startup();
        expect(player.current).toEqual("waiting");
    });
});
describe("player waiting", function() {
    beforeEach(function() {
        playerStub = jasmine.createSpyObj("player", ['Play', 'Stop', 'Pause']);
        player = new PlayerStateMachine(playerStub);
        // force the state to waiting
        player.current = "waiting";
    });
    it("should not play of we have don't have a url to play", function() {
        player.play();
        expect(player.current).toEqual("waiting");
    });
    it("should play if we have something to play", function() {
        player.addToPlayList("I'm a url");
        player.play();
        expect(playerStub.Play).toHaveBeenCalledWith("I'm a url");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a url");
    });
    it("should play the now playing if set", function() {
        // inject now playing items...
        player.playList = ["I'm a url", "I'm a teapot", "end of list"];
        player.nowPlaying = "I'm a teapot";
        player.play();
        expect(playerStub.Play).toHaveBeenCalledWith("I'm a teapot");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a teapot");
    });
    it ("should clear the playlist", function(){
        player.playList = ["I'm a url", "I'm a teapot", "end of list"];
        player.nowPlaying = "I'm a teapot";
        player.clearPlayList();
        expect(player.current).toEqual("waiting");
        expect(player.nowPlaying).toBeNull();
        expect(player.playList).toEqual([]);
    });
});
describe("player playing", function() {
    beforeEach(function() {
        playerStub = jasmine.createSpyObj("player", ['Play', 'Stop', 'Pause', 'SetPlaybackSpeed']);
        player = new PlayerStateMachine(playerStub);
        // force the state to waiting
        player.playList = ["I'm a url", "I'm a teapot", "end of list"];
        player.nowPlaying = "I'm a teapot";
        player.current = "playing";
    });
    it("should stop if playing", function()
    {
        player.stop();
        expect(playerStub.Stop).toHaveBeenCalled();
        expect(player.current).toEqual("waiting");
        expect(player.nowPlaying).toEqual("I'm a teapot");
    });
    it("should pause if playing", function()
    {
        player.pause();
        expect(playerStub.Pause).toHaveBeenCalled();
        expect(player.current).toEqual("paused");
        expect(player.nowPlaying).toEqual("I'm a teapot");
    });
    it("should skip to next if playing", function()
    {
        player.skipForward();
        expect(playerStub.Play).toHaveBeenCalledWith("end of list");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("end of list");
    });
    it("should skip to first in playlist if at end of list", function() {
        player.nowPlaying = "end of list";
        player.skipForward();
        expect(playerStub.Play).toHaveBeenCalledWith("I'm a url");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a url");

    });
    it("should skip to previous if playing", function()
    {
        player.skipBackward();
        expect(playerStub.Play).toHaveBeenCalledWith("I'm a url");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a url");
    });
    it("should skip backwards to end in playlist if at start of list", function() {
        player.nowPlaying = "I'm a url";
        player.skipBackward();
        expect(playerStub.Play).toHaveBeenCalledWith("end of list");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("end of list");
    });
    it("should add to playlist if playing", function() {
        player.addToPlayList("anotherItem");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a teapot");
        expect(player.playList).toEqual(["I'm a url", "I'm a teapot", "end of list", "anotherItem"]);
    });
    it("should fast forward if playing", function()
    {
        player.speedUp();
        expect(playerStub.SetPlaybackSpeed).toHaveBeenCalledWith(2);
        expect(player.current).toEqual("seeking");

    });
    it("should rewind if playing", function()
    {
        player.speedDown();
        expect(playerStub.SetPlaybackSpeed).toHaveBeenCalledWith(-2);
        expect(player.current).toEqual("seeking");
    });
    it("should play next if complete", function() {
        player.complete();
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("end of list");
    });
    it("should go to waiting if complete", function() {
        player.nowPlaying = "end of list";
        player.complete();
        expect(player.current).toEqual("waiting");
        expect(player.nowPlaying).toBeNull();
        expect(player.playList).toEqual([]);
    });
});
describe("player paused", function() {
    beforeEach(function() {
        playerStub = jasmine.createSpyObj("player", ['Play', 'Stop', 'Pause']);
        player = new PlayerStateMachine(playerStub);
        // force the state to waiting
        player.playList = ["I'm a url", "I'm a teapot", "end of list"];
        player.nowPlaying = "I'm a teapot";
        player.current = "paused";
    });
    it("should resume when play", function() {
        player.play();
        expect(playerStub.Play).toHaveBeenCalledWith("I'm a teapot");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a teapot");
    });
    it("should addToPlaylist", function() {
        player.addToPlayList("anotherItem");
        expect(player.current).toEqual("paused");
        expect(player.nowPlaying).toEqual("I'm a teapot");
        expect(player.playList).toEqual(["I'm a url", "I'm a teapot", "end of list", "anotherItem"]);
    });
});
describe("player seeking", function() {
    beforeEach(function() {
        playerStub = jasmine.createSpyObj("player", ['Play', 'Stop', 'Pause', 'SetPlaybackSpeed']);
        player = new PlayerStateMachine(playerStub);
        // force the state to waiting
        player.playList = ["I'm a url", "I'm a teapot", "end of list"];
        player.playbackSpeed = 2;
        player.nowPlaying = "I'm a teapot";
        player.current = "seeking";
    });
    it("should play", function() {
        player.play();
        expect(playerStub.Play).toHaveBeenCalledWith("I'm a teapot");
        expect(player.current).toEqual("playing");
        expect(player.nowPlaying).toEqual("I'm a teapot");
        expect(player.playbackSpeed).toEqual(0);
    });
    it("should increase speed", function() {
        player.speedUp();
        expect(playerStub.SetPlaybackSpeed).toHaveBeenCalledWith(4);
        expect(player.current).toEqual("seeking");
    });
    it("should decrease speed (avoid zero)", function() {
        player.speedDown();
        expect(playerStub.SetPlaybackSpeed).toHaveBeenCalledWith(-2);
        expect(player.current).toEqual("seeking");
    });
    it("should increase speed (avoid zero)", function() {
        player.playbackSpeed = -2;
        player.speedUp();
        expect(playerStub.SetPlaybackSpeed).toHaveBeenCalledWith(2);
        expect(player.current).toEqual("seeking");
    });
    it("should stop", function() {
        player.stop();
        expect(playerStub.Stop).toHaveBeenCalled();
        expect(player.current).toEqual("waiting");
    });
});

